<script setup>
import { ref, computed } from 'vue'
import { usePopStore } from '@/stores'
import { storeToRefs } from 'pinia'
const popStore = usePopStore()
const { Airspace, XY } = storeToRefs(popStore)
const close = () => {
  Airspace.value.showPop = false
}

const currentDom = ref(null)
const dialogPosition = computed(() => {
  const moveContainer = document.body
  //手动填写的位置不做处理
  let left = XY.value[0]
  let top = XY.value[1]

  const mw = moveContainer.offsetWidth
  const mh = moveContainer.offsetHeight
  if (!currentDom.value) return [0, 0]
  const ew = currentDom.value.offsetWidth
  const eh = currentDom.value.offsetHeight
  if (left < 0) {
    left = 0
  }
  if (top < 0) {
    top = 0
  }
  if (left > mw - ew) {
    left = mw - ew
  }
  if (top > mh - eh) {
    top = mh - eh
  }
  return [left, top]
})
</script>
<template>
  <transition
    appear
    name="custom-classes-transition"
    enter-active-class="animate__animated  animate__fadeIn"
    leave-active-class="animate__animated animate__faster  animate__fadeOut"
  >
    <div
      class="AirspacePop"
      ref="currentDom"
      :style="{
        left: dialogPosition[0] + 'px',
        top: dialogPosition[1] + 'px'
      }"
      v-if="Airspace.showPop"
    >
      <p class="title">
        <span>空域属性信息</span>
        <span @click="close"><img src="@img/pop/close_icon.png" alt="" /></span>
      </p>
      <div class="content">
        <div class="left">
          <p class="name">{{ Airspace.popInfo.airspaceName }}</p>
          <p>
            <span>· 空域面积：</span><span>{{ Airspace.popInfo.airspaceArea }} km²</span>
          </p>
          <p>
            <span>· 空域底高：</span><span>{{ Airspace.popInfo.botHeight }} m</span>
          </p>
          <p>
            <span>· 空域顶高：</span><span>{{ Airspace.popInfo.topHeight }} m</span>
          </p>
          <p>
            <span>· 空域用途：</span><span>{{ Airspace.popInfo.airspaceType }}</span>
          </p>
        </div>
        <div class="right">
          <img src="@img/pop/airspaceimg.jpg" alt="" />
        </div>
      </div>
    </div>
  </transition>
</template>
<style scoped lang="scss">
.AirspacePop {
  position: fixed;
  z-index: 9;
  top: 50%;
  left: 50%;
  width: 328px;
  height: 219px;
  background: url('@img/pop/airspace_bg.png');
  background-size: 100% 100%;

  .title {
    display: flex;
    padding: 10px 20px 10px 50px;
    align-items: center;
    font-size: 16px;
    justify-content: space-between;

    span:last-child {
      width: 18px;
      height: 18px;
      cursor: pointer;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .content {
    margin-top: 5px;
    padding: 12px;
    display: flex;
    font-size: 13px;

    .left {
      flex: 1;

      p {
        padding: 5px 0;
        span:first-child {
          color: #afebff;
        }

        &.name {
          font-size: 14px;
        }
      }
    }

    .right {
      flex: 0 0 120px;
      margin-left: 5px;

      img {
        width: 100%;
      }
    }
  }
}
</style>
